package org.example.gradlegateway.filter;

import io.micrometer.tracing.Tracer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


/**
 * 定义全局filter
 */
@Slf4j
@Component
public class MyDemoGlobalFilter implements GlobalFilter {

	@Resource
	private Tracer tracer;

	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		//无效的传递，下游服务收不到ThreadLocal的数据，因为是通过http传递访问不在一个线程中
//		BaseContextHandler.set("BaseContextHandler key", "BaseContextHandler key");

		log.info("custom global filter traceId {}", tracer.currentSpan().context().traceId());

		return chain.filter(exchange);
	}

}